package com.sys.orders.back.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.sys.common.controller.BaseController;
import com.sys.common.pojo.ApiResponse;
import com.sys.common.util.ConstatFinalUtil;
import com.sys.common.util.PageInfoUtil;
import com.sys.goods.pojo.AAddress;
import com.sys.goods.pojo.ABlindbox;
import com.sys.goods.service.IGoodsDbService;
import com.sys.orders.pojo.AOrdersDesc;
import com.sys.orders.pojo.AOrdersGoods;
import com.sys.orders.service.IOrdersDbService;
import com.sys.users.pojo.AUsers;

@Controller
@RequestMapping("/back/ordersgoods")
public class ordersGoodsBackController extends BaseController
{
	@Autowired
	private IOrdersDbService ordersDbService;
	
	@Autowired
	private IGoodsDbService goodsDbService ;

	/**
	 * 订单明细列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/ordersGoodsList")
	public String ordersGoodsList(HttpServletRequest request, HttpServletResponse response)
	{
		ConstatFinalUtil.SYS_LOGGER.info("====ordersGoodsList======");
		/* 生成分页对象 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 生成查询条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		
		String orderId = request.getParameter("orderId");
		if(orderId == null) {
			orderId = "" ; 
		}
		condMap.put("orderId", orderId);

		ApiResponse<AOrdersGoods> ordersGoodsList = this.ordersDbService.findCondListAOrdersGoodsService(pageInfoUtil,
				condMap);

		request.setAttribute("response", ordersGoodsList.toJSON());
		request.setAttribute("pageInfoUtil", pageInfoUtil);

		return "/back/order/ordersGoodsList";
	}

	/**
	 * 订单明细插入
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/ordersGoodsInsert")
	public String ordersGoodsInsertt(HttpServletRequest request, HttpServletResponse response)
	{
		ConstatFinalUtil.SYS_LOGGER.info("=======ordersGoodsInsert========");
		return "/back/order/ordersGoodsInsert";
	}

	/**
	 * 订单明细插入提交
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/ordersGoodsInsertSubmit")
	public String ordersGoodsInsertSubmit(HttpServletRequest request, HttpServletResponse response)
	{
		ConstatFinalUtil.SYS_LOGGER.info("=======ordersGoodsInsertSubmit========");
		/* 接收参数 */
		String orderId = request.getParameter("orderId");
		String blindBoxId = request.getParameter("blindBoxId");
		String settlementPrice = request.getParameter("settlementPrice");
		String goodsQuantity = request.getParameter("goodsQuantity");
		String status = request.getParameter("status");

		/* 创建对象 */
		AOrdersGoods ordersGoods = new AOrdersGoods();

		ordersGoods.setOrderId(Integer.valueOf(orderId));
		ordersGoods.setBlindBoxId(Integer.valueOf(blindBoxId));
		ordersGoods.setSettlementPrice(Integer.valueOf(settlementPrice));
		ordersGoods.setGoodsQuantity(Integer.valueOf(goodsQuantity));
		ordersGoods.setStatus(Byte.valueOf(status));

		ApiResponse<Object> apiResponse = this.ordersDbService.saveOneOrdersGoodsService(ordersGoods);

		return apiResponse.toJSON().toJSONString();
	}

	/**
	 * 订单明细更新
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/ordersGoodsUpdate")
	
	public String ordersGoodsUpdate(HttpServletRequest request, HttpServletResponse response)
	{
		ConstatFinalUtil.SYS_LOGGER.info("=======ordersGoodsUpdate========");

		String ordersId = request.getParameter("ordersId");
		String operType = request.getParameter("operType");

		Map<String, Object> condMap = new HashMap<String, Object>();

		condMap.put("ordersId", Integer.valueOf(ordersId));
		ApiResponse<AOrdersGoods> goods = this.ordersDbService.findOneAOrdersGoodsService(condMap);

	    AOrdersGoods dataOneJava = goods.getDataOneJava();
	   
	    condMap.clear();
		condMap.put("orderId",dataOneJava.getOrderId());
		condMap.clear();
		condMap.put("blindBoxId",dataOneJava.getBlindBoxId());

		ApiResponse<AOrdersDesc> orderId = this.ordersDbService.findOneOrdersDescService(condMap);
		ApiResponse<ABlindbox> blindBoxId = this.goodsDbService.findOneABlindboxService(condMap);
		
		request.setAttribute("goods", goods.toJSON());
		request.setAttribute("orderId", orderId.toJSON());
		request.setAttribute("blindBoxId", blindBoxId.toJSON());
		
		if ("update".equalsIgnoreCase(operType))
		{
			
			return "/back/order/ordersGoodsUpdate";
		}
		
		return "/back/order/ordersGoodsInfo";
	}

	/**
	 * 订单明细更新提交
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/ordersGoodsUpdateSubmit")
	@ResponseBody
	public String ordersGoodsUpdateSubmit(HttpServletRequest request, HttpServletResponse response)
	{
		ConstatFinalUtil.SYS_LOGGER.info("=======ordersGoodsUpdateSubmit========");

		String orderDetailsId = request.getParameter("orderId");

		Map<String, Object> condMap = new HashMap<String, Object>();

		condMap.put("orderDetailsId", Integer.valueOf(orderDetailsId));
		ApiResponse<AOrdersGoods> goods = this.ordersDbService.findOneAOrdersGoodsService(condMap);

		AOrdersGoods ordersGoods = goods.getDataOneJava();

		/* 接收参数 */
		
		String status = request.getParameter("status");

		
		ordersGoods.setStatus(Byte.valueOf(status));
		
		ApiResponse<Object> apiResponse = this.ordersDbService.updateOneOrdersGoodsService(ordersGoods);
		ConstatFinalUtil.SYS_LOGGER.info(apiResponse.toJSON().toJSONString());
		
		JSONObject resultJSON = apiResponse.toJSON();
		return resultJSON.toJSONString();
	}
}
